﻿using System;
using System.Collections;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;

namespace NetTopologySuite.Operation.Linemerge
{
	public class EdgeString
	{
		public EdgeString(IGeometryFactory factory)
		{
			this.factory = factory;
		}

		public void Add(LineMergeDirectedEdge directedEdge)
		{
			this.directedEdges.Add(directedEdge);
		}

		private Coordinate[] Coordinates
		{
			get
			{
				if (this.coordinates == null)
				{
					int num = 0;
					int num2 = 0;
					CoordinateList coordinateList = new CoordinateList();
					foreach (object obj in this.directedEdges)
					{
						LineMergeDirectedEdge lineMergeDirectedEdge = (LineMergeDirectedEdge)obj;
						if (lineMergeDirectedEdge.EdgeDirection)
						{
							num++;
						}
						else
						{
							num2++;
						}
						coordinateList.Add(((LineMergeEdge)lineMergeDirectedEdge.Edge).Line.Coordinates, false, lineMergeDirectedEdge.EdgeDirection);
					}
					this.coordinates = coordinateList.ToCoordinateArray();
					if (num2 > num)
					{
						CoordinateArrays.Reverse(this.coordinates);
					}
				}
				return this.coordinates;
			}
		}

		public ILineString ToLineString()
		{
			return this.factory.CreateLineString(this.Coordinates);
		}

		private readonly IGeometryFactory factory;

		private readonly IList directedEdges = new ArrayList();

		private Coordinate[] coordinates;
	}
}
